package exabase.server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

import exabase.Exabase;
import exabase.com.RequestProcessor;

public class Server {
	private static Logger logger = Logger.getLogger(Exabase.class.getName());
	private static int serverPort = 4444;
	private static int numThreads = 4;

	public static void main(String[] args) {
		ServerSocket serverSocket = null;
		try {
			serverSocket = new ServerSocket(serverPort);
			logger.fine("Server socket opened on port " + serverPort);
		} catch (IOException e) {
			logger.log(Level.SEVERE, "Could not listen on port: 4444.", e);
			System.exit(1);
		}

		for (int i = 0; i < numThreads; i++) {
			Thread t = new Thread(new RequestProcessor());
			t.start();
		}

		while (true) {
			try {
				Socket request = serverSocket.accept();
				logger.finer("accept");
				RequestProcessor.processRequest(request);
			} catch (IOException ex) {
				logger.log(Level.WARNING,
						"Soething went wrong when opening a server socket", ex);
			}
		}
	}
}
